package com.bzs.batch.reconcile.batch.itemreader;

import com.bzs.batch.reconcile.entity.HostFile;
import com.bzs.batch.reconcile.entity.ReconcileBatch;
import com.bzs.batch.reconcile.mapper.ReconcileBatchMapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileUrlResource;

/**
 * @Classname Itemreaderfile
 * @Author baozs
 * @Date 2021/12/22 11:40
 * @Version 1.0
 * @Description
 **/
@Configuration
@AllArgsConstructor
public class ItemReaderFile {
    private final ReconcileBatchMapper reconcileBatchMapper;
    @Bean
    @StepScope
    public FlatFileItemReader<HostFile> getFileItemReader(@Value("#{jobParameters[date]}") String date) {

        FlatFileItemReader<HostFile> reader = new FlatFileItemReader<>();
        try {
            reader.setResource(new FileUrlResource("C:\\project\\progect_reconcil\\HOST\\2021122000000001.txt"));
        } catch (Exception e) {
            System.out.println("读取文件异常");
        }
        reader.setEncoding("UTF-8");
        reader.setLinesToSkip(1);
        //数据解析
        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
        //分隔符
        tokenizer.setDelimiter("$");
        //全局流水号$核心流水号$借方账号$贷方账号$核心日期$核心时间$交易结果$交易金额
        tokenizer.setNames("allSerialno", "hostSeriano", "DebitAccount", "credit", "tradeData", "tradeTime", "hostTradeResult", "transAmount");
        //把解析出的一行数据映射为对象
        DefaultLineMapper<HostFile> mapper = new DefaultLineMapper<>();
        mapper.setLineTokenizer(tokenizer);
        mapper.setFieldSetMapper(fieldSet -> {
            HostFile hostFile = new HostFile();
            if (StringUtils.isNotEmpty(fieldSet.readString("allSerialno"))) {
                hostFile.setAllSerialno(fieldSet.readString("allSerialno"));
                System.out.println("文件读取成功，流水号：" + hostFile.getAllSerialno());
            }
            if (StringUtils.isNotEmpty(fieldSet.readString("hostSeriano"))) {
                hostFile.setHostSeriano(fieldSet.readString("hostSeriano"));
            }
            if (StringUtils.isNotEmpty(fieldSet.readString("DebitAccount"))) {
                hostFile.setDebitAccount(fieldSet.readString("DebitAccount"));
            }
            if (StringUtils.isNotEmpty(fieldSet.readString("credit"))) {
                hostFile.setCredit(fieldSet.readString("credit"));
            }
            if (StringUtils.isNotEmpty(fieldSet.readString("tradeData"))) {
                hostFile.setTradeData(fieldSet.readString("tradeData"));
            }
            if (StringUtils.isNotEmpty(fieldSet.readString("tradeTime"))) {
                hostFile.setTradeTime(fieldSet.readString("tradeTime"));
            }
            if (StringUtils.isNotEmpty(fieldSet.readString("hostTradeResult"))) {
                hostFile.setHostTradeResult(fieldSet.readString("hostTradeResult"));
            }
            if (StringUtils.isNotEmpty(fieldSet.readString("transAmount"))) {
                hostFile.setTransAmount(fieldSet.readString("transAmount"));
            }
            return hostFile;
        });
        mapper.afterPropertiesSet();
        reader.setSkippedLinesCallback(line -> {
            //文件中读取到的第一行数据,存到数据库reconcileBatchMapper表
            String[] values = StringUtils.split(line, "$");
            ReconcileBatch reconcileBatch = new ReconcileBatch();
            reconcileBatch.setBatchNo(date);
            reconcileBatch.setBatchCount(values[0]);
            reconcileBatch.setHatchAmount(values[1]);
            int insert = reconcileBatchMapper.insert(reconcileBatch);
            if (insert>0){
                System.out.println("文件头保存成功：总金额："+values[1]+"总笔数："+values[0]);
            }
        });
        reader.setLineMapper(mapper);
        return reader;
    }
}
